package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/*
* 员工管理
* */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login") //管理端登录
    public R<Employee> login(HttpServletRequest request , @RequestBody Employee employee) {
        /*
        * 将用户提交的密码进行加密处理
        * 根据页面提交的用户名查询数据库*/

        String password = employee.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Employee::getUsername,employee.getUsername());

        Employee one = employeeService.getOne(wrapper);

        /*对返回结果进行判断:
        * 1 未查询到 返回登录失败
        * 2 判断密码是否正确,错误则返回登录失败
        * 3 判断员工是否被禁用,禁用则返回登陆失败结果
        * 4 登陆成功,将员工的id存入Session 并返回登录成功的结果*/

        if (one == null){
            return R.error("用户不存在");
        }

        if (!one.getPassword().equals(password)){
            return R.error("登陆失败,密码错误");
        }

        if (one.getStatus() == 0){
            return R.error("该用户已被禁用");
        }

        request.getSession().setAttribute("employee",one.getId());

        return R.success(one);
    }

    @PostMapping("/logout") //管理端退出
    public R<String> logout(HttpServletRequest request){
        /*
        * 清理Session中保存的当前员工的id*/
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping //新增员工
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工,员工信息: {}",employee.toString());

        //设置初始密码,并进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获得登录用户的id
        Long o = (Long)request.getSession().getAttribute("employee");

        employee.setCreateUser(o);

        employee.setUpdateUser(o); //更新人*/

        //调用业务层方法
        employeeService.save(employee);
        //返回数据
        return R.success("新增成功");

    }

    @GetMapping("/page") //员工分页查询
    public R<Page> page(int page, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //创建一个条件构造器对象
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        //创建一个分页构造器对象
        Page p = new Page(page, pageSize);

        //添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //添加排序条件
        wrapper.orderByAsc(Employee::getUpdateTime);

        //调用业务层方法进行查询
        employeeService.page(p,wrapper);
        return R.success(p);
    }

    @GetMapping("/{id}") //根据用户id查询用户信息
    public R<Employee> getById(@PathVariable Long id){
        log.info("通过id查询用户信息...");
        //调用方法查询信息
        Employee employee = employeeService.getById(id);

        //对查询的用户信息进行判断
        if (employee != null){
            return R.success(employee);
        }
        //当查询到的数据为空时,返回错误的结果信息
        return R.error("未查询到该用户信息");
    }

    @PutMapping //编辑用户信息
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
        log.info(employee.toString());

        /*//对当前经过的线程进行打印
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);*/

        /*Long emp = (Long) request.getSession().getAttribute("employee");
        //修改完成的用户信息
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(emp);*/
        //调用方法完成修改
        employeeService.updateById(employee);

        return R.success("信息修改成功!");
    }
}
